;(function($){
	$.spa_front_account_search = $.spa_front_account_search || {}
	$.fn.spa_front_account_search = function(opts){
		var el = this;
		var opt = {
			ct_brzd : {},
			ct_brxsxm : [],
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.spa_front_account_search = $.extend($.spa_front_account_search,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//清空
			el.find("#btnClear").unbind("click").click(function(){
				t.clearAccountInfo();
			})
			
			//查询按钮-------选择手牌模态
//			el.find("#btnSearch").unbind("click").click(function(){
//				t.thSearch();
//			})
			
			el.find("#th").unbind("keyup").keyup(function(){
				var value = $(this).val();
				if(value.toString().length==4){
					$.spa_front_account_menu.param.th = value;
					t.thSearch();
				}else{
					$.spa_front_account_menu.param.th = "";
				}
			})
			
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = 	'<div id="searchCentent">'+
							'<div class="widget-box">'+
								'<div class="widget-body">'+
									'<div class="widget-main" style="text-align:center;zoom:120%;">'+
										'<input type="text" id="th" name="th" class="" />'+
										'<div class="btn-group" style="margin-top:-4px;">'+
											'<button id="btnClear" class="btn btn-sm btn-info">'+
												'<i class="ace-icon fa fa-trash"></i>'+
												$.i18n.propJsp("清空")+
											'</button>'+
											'<button id="btnSearch" class="btn btn-sm btn-info">'+
												'<i class="ace-icon fa fa-search"></i>'+
												$.i18n.propJsp("选择")+
											'</button>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div id="numberLetters" class="widget-box" style="height:302px;">'+
								
							'</div>'+
							'<div id="accountItems" class="widget-box">'+
								'<div id="" class="widget-body">'+
									'<div id="accountList" class="widget-main no-padding"></div>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<div id="" class="widget-box" style="height:63px;">'+
							'<div id="pageTop">'+
								'<div id="pageTopLeft">'+
									'<span id="pageThBox" style="width:40%;height: 72px;padding:5px;padding-left:10px;line-height: 62px;">'+
										'<span id="pageTh" class="width-90 label label-info label-xlg arrowed-right" style="zoom: 150%;font-size:12px;padding:0px;line-height: 26px;font-size:12px;color:white;font-weight: bold;">'+
										' </span>'+
									'</span>'+
									'<span class="" style="width:60%;">'+
										'<span class="col-sm-12" style="border-bottom: 1px dotted #ccc;height: 36px;line-height: 36px;">'+
											'<span class="col-sm-3" data-i18n="NO.">NO.</span>'+
											'<span class="col-sm-9">'+
												'<span id="pageNo" style="cursor:pointer;">0</span>'+
											'</span>'+
										'</span>'+
										'<span class="col-sm-12" style="height: 36px;line-height: 36px;">'+
											'<span class="col-sm-3" data-i18n="金额">'+$.i18n.propJsp("金额")+'</span>'+
											'<span class="col-sm-9">'+
												'<span id="pageJe">0.00</span>'+
											'</span>'+
										'</span>'+
									'</span>'+
								'</div>'+
								'<div id="pageTopRight">'+
									'<div class="col-sm-12 no-padding">'+
										'<div class="col-sm-12 no-padding" style="border-bottom: 1px dotted #ccc;height: 36px;float:left;">'+
											'<div class=" " style="padding: 6px 10px 6px 4px;text-align: right;width:100px;float:left;">'+
												$.i18n.propJsp('接待号:')+
											'</div>'+
											'<div id="pageJdh" class=" " style="padding: 6px 10px 6px 4px;text-align: left;float:left;">'+
											'</div>'+
											'<div id="pageInfo1" class=" " style="padding: 6px 10px 6px 4px;text-align: left;float:left;">'+
											'</div>'+
										'</div>'+
										'<div class="col-sm-12 no-padding">'+
											'<div class="col-sm-2 " style="padding: 6px 10px 6px 4px;text-align: right;width:100px;float:left;">'+
												$.i18n.propJsp('备注:')+
											'</div>'+
											'<div id="pageInfo2" class="col-sm-10 " style="padding: 6px 10px 6px 4px;text-align: left;">'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							
						'</div>';
			el.html(html);
			if(!$.isFunction($().number_letters_keyboard)){
				$.ajaxSetup({async: false,cache: false});
	            $.getScript("js/spa_guestList/jquery.service.number_letters_keyboard.js");
	            $.ajaxSetup({async: true});
			}
			el.find("#numberLetters").number_letters_keyboard({
				dataBack : function(value){
					var value_l = el.find("#th").val();
					if(value_l.length==4){
						value = value
					}else{
						value = el.find("#th").val()+value
					}
					el.find("#th").val(value);
					if(value.toString().length==4){
						$.spa_front_account_menu.param.th = value;
						t.thSearch();
					}else{
						$.spa_front_account_menu.param.th = "";
					}
				}
			})
			t.setSize();
		},
		setSize : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
//			var winH = $(window).height();
//			var top = el.find("#accountItems").offset().top;
//			var h = winH - top - 69;
//			el.find("#accountList").css({"height":h,"overflow-y":"auto","overflow-x":"hidden"});
			var offset = parseFloat($(".page-content").css("padding-top"));//页面偏差
			var winh = $(window).height();
			var top = el.find("#searchCentent").offset().top;
			var h = winh - top - 69 + offset;
			el.find("#searchCentent").css({"height":h});
			var top1 = el.find("#accountItems").offset().top;
			var hh = h - (top1 - top)
			el.find("#accountList").css({"height":hh,"overflow-y":"auto","overflow-x":"hidden"});
		},
		thSearch : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var th = el.find("#th").val();
			var arg = {"arg":{"th":th}}
			param.wait();
			jQuery.ajax({  
	    		type : 'POST',
	    		url : $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
	    		data : $.HSAPI_ASKJSONINFO(JSON.stringify(arg),'SPAOrderShoupai_Search','{}'),
	    		dataType : 'json',  
	    		success : function(data){
	    			param.goon();
//	    			console.log(data)
	    			if(data.RETURN==1){
	    				param.ct_brzd = data.ct_brzd[0];
	    				param.brzdBack(param.ct_brzd);
	    				param.ct_brxsxm = data.ct_brxsxm;
	    				t.initAccountItems();
	    				param.dataBack(data);
//	    				el.find(".bigger.total").text(data.amount[0].je);
//	   					el.find(".pay_p").text("￥"+ data.amount[0].balance);
	   					t.initAccount();
	    			}else{
						if(data.hasOwnProperty("gritter")){
							if(data.hasOwnProperty("gritterstyle"))
								jf_successDelayed(data.gritter,2000,data.gritterstyle);
							else
								jf_successDelayed(data.gritter,2000); 
						}else{
		    				jf_alert(data.MSG)
						}
					}
	    		},  
	    		error : function(data){
	    			param.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		initAccountItems : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.find("#accountList").empty();
			$(param.ct_brxsxm).each(function(k,v){
				t.addAccountItems(v);
			})
		},
		addAccountItems : function(json){
			var el = this.el;
			var param = this.param;
			var t = this;
			
//			var badgeHtml = "";
			var index = el.find("#accountList").find(".each_undish").size()+1;
//			var html='<div class="each_undish" id="'+json.bh+'" data-bh="'+json.bh+'">'+
//						'<div class="btn btn-app btn-light no-radius dish_content" data-index="'+index+'" data-id="tableView">'+
//							'<div class="jsone hide">'+ JSON.stringify(json) +'</div>'+
//							'<div class="each_top flex">'+
//								'<div class="eachName flex">'+
//									'<div class="dish_id">'+ index +'</div>.&nbsp;'+
//									'<div data-name="'+json.mc+'" class="dish_name">'+ json.mc +'</div>&nbsp;'+
//								'</div>'+
//								'<div class="eachRight flex">'+//添加羡慕
//									'<div class="eachPirce">'+ toFloot(json.je) +'/'+ json.dw +'</div>×'+
//									'<div class="eachNumber">'+json.sl+'</div>'+
//								'</div>'+
//							'</div>'+ 
//							'<div class="each_bottom flex">'+//做法备注
//							'</div>'+
//						'</div>'+
//						'<div class="sub_dishes"></div>'+
//					'</div>';
//			el.find("#accountList").append(html);
			
			
			var bz=""
			var bzclass=""
			var sl=1
			if(!json.bz && !json.tsyq){
				bzclass=" hide"//无数据隐藏
			}else if(!!json.bz && !json.tsyq){
				bz=json.bz
			}else if(!json.bz && !!json.tsyq){
				bz=json.tsyq
			}else{
				bz=json.tsyq + json.bz
			}
			if(typeof(json.sl)!="undefined"){//数量
				var sl = json.sl.split(".");
				if(parseInt("0." + sl[1]) > 0){
					sl=value.sl;
				}else{
					sl=sl[0]+"."+sl[1];						
				}
			}
			var tcclass=""//是否显示展开套餐
			var add=""//是否显示增加套餐明细
			var color = "#2b8ccc;"
			var mc = ""
			var istc = "";
			
			if(json.suit_bj!=1&&json.suit_bj!=3){//为套餐
				tcclass="hide"
			}
			
			if(json.suit_bj==3){//suit_bj为3才允许编辑套餐
				add=" add"
			}
			
			var jeStatus = "hide"
			if(typeof(json.status_c)=="string"&&json.status_c!="{}"){
				json.status_c = $.parseJSON(json.status_c);
			}
			var showRevHtml= ""
			if(json.status_c.length>0){
				$(json.status_c).each(function(k,v){
					if(JSON.stringify(v)!="{}"){
						showRevHtml += '<span class="badge" style="background:'+(v.bgcolor||"#00FF00")+' !important;color:'+(v.scolor||"black")+';">'+v.name+'</span>';
					}
				})
			}
			
			var sortStatus = ""
			if(json.sort!=0){//sort=0显示折退等标记
				sortStatus = "hide";
			}
			
			if(json.sort==1){
				color = "#999";
			}
			var html='<div class="each_undish" id="'+json.bh+'" data-bh="'+json.bh+'">'+
						'<div class="jsone hide">'+ JSON.stringify(json) +'</div>'+ 
						'<div class="btn btn-app btn-light no-radius dish_content '+istc+'" data-id="tableView" style="padding:0px !important">'+ 
							'<div class="each_top flex">'+
								'<div class="eachName flex">'+
									'<div class="dish_id" style="color:'+color+'">'+ index +'</div>&nbsp;<div class="dish_name" style="color:'+color+'">'+ json.mc_c +'</div>'+
								'</div>'+
								'<div class="eachRight flex">'+
									'<div class="eachPirce">'+ toFloot(json.dj) +'/'+ json.dw +'</div>×'+ 
									'<div class="dish_name">'+sl+'</div>'+
								'</div>'+
							'</div>'+
							'<div class="each_top flex '+sortStatus+'" style="float: right;width: 50%;">'+
								'<div class="eachRight flex">'+
									'<div class="dish_name" style="color:'+color+'">'+ '<span class="'+jeStatus+'" style="width:90px;dispaly:block;">' + json.je_c + '</span>' +
									showRevHtml +
									'<span style="width:90px;dispaly:block;">' +json.netamt + '</span>' + '</div>'+
								'</div>'+
							'</div>'+
							'<div class="each_bottom flex" style="float: left;width: 50%;margin-top: 7px;overflow:hidden;text-overflow:ellipsis;">'+  
//									'<div class="badge badge-pink showtc '+tcclass+'">'+json.is_tc+'</div>'+
								'<div class="eachRemark'+bzclass+'">'+ bz +'</div>'+
							'</div>'+
						'</div>'+
						'<div class="sub_dishes"></div>'+
					'</div>'
			var bharr=[]//收集bh数组
			if(json.suit_bj!=1&&json.suit_bj!=3&&json.is_manyunit==0){//非套餐非多规格
				el.find("#accountList").append(html);
			}else if(json.suit_bj==1||json.suit_bj==3){//套餐
				el.find("#accountList").append(html);
				//添加套餐列表
				if(typeof(json.tc_detail)=="string"){
					var dat=JSON.parse(json.tc_detail)
//					$(dat).each(function(k,vv){
//						var html='<div class="profile-activity sub_each" id="'+vv.bh+'" data-id="sub_meal">'+
//									'<ul class="dropdown-menu dropdown-success dropdown-menu-right">'+
//										'<li class="delete_sub">'+
//											'<a href="#"><i class="ace-icon fa fa-trash-o bigger-120"></i>'+
//											'<span class="menu-text">'+$.i18n.propJsp("删除")+' </span></a>'+
//										'</li>'+
//									'</ul>'+
//									'<div class="sub_name">'+vv.mc+'</div>'+
//									'<div class="sub_right"><div class="sub_pri" style="color:#9585BF;">'+vv.dj+'</div>×'+
//										'<div class="sub_number">'+(vv.sl).split(".")[0]+'</div>'+
//									'</div>'+
//								'</div>'
//						el.find("#accountList").find("#"+json.bh+"").find(".sub_dishes").append(html)
//					})
				}else{
					$(json.tc_detail).each(function(k,v){
						var html='<div class="profile-activity sub_each" id="'+v.bh+'" data-id="sub_meal">'+
									'<ul class="dropdown-menu dropdown-success dropdown-menu-right">'+
										'<li class="delete_sub">'+
											'<a href="#"><i class="ace-icon fa fa-trash-o bigger-120"></i>'+
											'<span class="menu-text"> '+$.i18n.propJsp("删除")+'  </span></a>'+
										'</li>'+
									'</ul>'+
									'<div class="sub_name">'+v.mc+'</div>'+
									'<div class="sub_right"><div class="sub_pri" style="color:#9585BF;">'+v.dj+'</div>×'+
										'<div class="sub_number">'+(v.sl).split(".")[0]+'</div>'+
									'</div>'+
								'</div>'
						el.find("#accountList").find("#"+son.bh).find(".sub_dishes").append(html)
					})
				}
			}else{//多规格
				el.find("#accountList").append(html);
			}
			t.setScrollPosition();
		},
		setScrollPosition : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var h = el.find("#accountList").prop('scrollHeight');
			el.find("#accountList").scrollTop(h);
		},
		clearAccountInfo : function(){//清除数据
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.find("#th").val("");
			param.ct_brzd = {};
			param.ct_brxsxm = [];
			el.find("#accountList").empty();
			param.brzdBack(param.ct_brzd);
			param.dataBack({ct_brzd:[{}],ct_brxsxm:[],amount:[{}],payment:[],ho_dcode:[]});
//			el.find(".bigger.total").text("0.00");
//			el.find(".pay_p").text("￥0.00");
			el.find("#pageNo").text("")
			el.find("#pageJe").html("0.00");
			el.find("#pageJdh").html("");
		},
		initAccount : function(){
			//统计金额
			var el = this.el;
			var param = this.param;
			var t = this;
			param.ct_brzd;
//			el.find("#pageThBox").html(th_text)
			el.find("#pageNo").text(param.ct_brzd.no)
			
			var je = '<span class="" style="">'+param.ct_brzd.xfje+'</span>'
			if(param.ct_brzd.dfje&&Number(param.ct_brzd.dfje)!=0){
				je += 	'<span class="" style="margin-left: 2px;color:#d15b47 !important;font-weight: bold;">('+param.ct_brzd.dfje+')</span>';
			}
			el.find("#pageJe").html(je);
			el.find("#pageJdh").html(param.ct_brzd.jdh||"");
		},
	}
})(jQuery);